package com.qut.graduate.busrtquery.utils.photoselect;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import com.qut.graduate.busrtquery.utils.common.FormatUtils;

/**
 * 图片相关工具类
 */
public class ImageUtils {

    /**
     * 获取Bitmap
     *
     * @param filePath 文件路径
     * @return bitmap
     */
    public static Bitmap getBitmap(final String filePath) {
        if (FormatUtils.isSpace(filePath))
            return null;
        return BitmapFactory.decodeFile(filePath);
    }

    /**
     * 从Uri中获取图片路径
     *
     * @param context
     * @param uri 图片Uri
     * @return
     */
    public static String getImagePathFromUri(Context context, Uri uri) {
        Cursor cursor = null;
        try {
            String[] pathData = {MediaStore.Images.Media.DATA};
            cursor = context.getContentResolver().query(uri, pathData, null, null, null);
            if (cursor != null) {
                int columnPath = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(columnPath);
            } else {
                return null;
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

}